﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Sinacor.Infra.Core.Licensing.Data
{
    public class Historico : ResourceAccess
    {
        #region Methods

        public int SelecionarProximoIdHistorico()
        {
            int retorno = 0;

            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                #region Query
                string sql = " SELECT NVL(MAX(NUM_SEQ_HIST_LICE),0)+1 FROM HFWDLICE ";
                #endregion

                command.CommandText = sql.ToString();
                retorno = Convert.ToInt32(this.ExecuteScalar(command));
            }

            return retorno;
        }

        public List<Sinacor.Infra.Core.Licensing.Description> Selecionar(Sinacor.Infra.Core.Licensing.Description library)
        {
            List<Sinacor.Infra.Core.Licensing.Description> response = null;

            StringBuilder sql = new StringBuilder();

            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                #region Query
                sql.AppendLine(@" SELECT HTC.NUM_SEQ_HIST_LICE
                                        ,THC.COD_TIPO_ATLZ_HIST
                                        ,THC.NOME_TIPO_HISTORICO
                                        ,HTC.COD_LICE
                                        ,HTC.COD_USUA
                                        ,HTC.DATA_HIST
                                        ,HTC.TXT_COME
                                  FROM  HFWDLICE HTC
                                  INNER JOIN TFWDTIPO_ATLZ_HIST THC
                                  ON HTC.COD_TIPO_ATLZ_HIST = THC.COD_TIPO_ATLZ_HIST
                                  WHERE (HTC.NUM_SEQ_HIST_LICE  = :NUM_SEQ_HIST_LICE  OR :NUM_SEQ_HIST_LICE IS NULL) 
                                  AND   (HTC.COD_TIPO_ATLZ_HIST = :COD_TIPO_ATLZ_HIST OR :COD_TIPO_ATLZ_HIST IS NULL) 
                                  AND   (HTC.COD_LICE           = :COD_LICE           OR :COD_LICE IS NULL) 
                                  AND   (HTC.COD_USUA           = :COD_USUA           OR :COD_USUA IS NULL) 
                                  AND   (HTC.DATA_HIST          = :DATA_HIST          OR :DATA_HIST IS NULL) 
                                  ORDER BY HTC.NUM_SEQ_HIST_LICE DESC");

                #endregion

                #region Filtro
                this.AddInParameter(command, "NUM_SEQ_HIST_LICE", DbType.Int32, library.Id);
                this.AddInParameter(command, "COD_TIPO_ATLZ_HIST", DbType.Int32, library.TypeDescription);
                this.AddInParameter(command, "COD_LICE", DbType.Int32, library.LicensingId);

                this.AddInParameter(command, "COD_USUA", DbType.String, library.User);
                this.AddInParameter(command, "DATA_HIST", DbType.DateTime, library.Period);

                #endregion

                command.CommandText = sql.ToString();
                response = this.FindAll<Sinacor.Infra.Core.Licensing.Description>(command,
                    (
                #region Dados
reader => new Sinacor.Infra.Core.Licensing.Description
{
    Id = reader.GetValue<int>("NUM_SEQ_HIST_LICE"),

    TypeDescription = (Sinacor.Infra.Core.Licensing.TypeDescription)reader.GetValue<int>("COD_TIPO_ATLZ_HIST"),
    LicensingId = reader.GetValue<int>("COD_LICE"),

    User = reader.GetValue<string>("COD_USUA"),
    Period = reader.GetValue<DateTime>("DATA_HIST"),
    Annotation = reader.GetValue<string>("TXT_COME")
}
                #endregion
)
                );
            }

            return response;
        }

        public void Incluir(Sinacor.Infra.Core.Licensing.Description library)
        {
            StringBuilder sql = new StringBuilder();
            using (IDbCommand command = this.GetSqlStringCommand(" "))
            {
                #region Query

                sql.AppendLine(@" INSERT INTO HFWDLICE
                                               (NUM_SEQ_HIST_LICE
                                               ,COD_TIPO_ATLZ_HIST
                                               ,NUM_SEQ_VERS_LICE
                                               ,COD_LICE
                                               ,COD_USUA
                                               ,DATA_HIST
                                               ,TXT_COME)
                                      VALUES
                                              (:NUM_SEQ_HIST_LICE
                                              ,:COD_TIPO_ATLZ_HIST
                                              ,:NUM_SEQ_VERS_LICE
                                              ,:COD_LICE
                                              ,:COD_USUA
                                              ,:DATA_HIST
                                              ,:TXT_COME) ");
                #endregion

                #region Dados
                this.AddInParameter(command, "NUM_SEQ_HIST_LICE", DbType.Int32, library.Id);
                this.AddInParameter(command, "COD_TIPO_ATLZ_HIST", DbType.Int32, library.TypeDescription);
                this.AddInParameter(command, "NUM_SEQ_VERS_LICE", DbType.Int32, library.IdVersionLicensing);
                this.AddInParameter(command, "COD_LICE", DbType.Int32, library.LicensingId);

                this.AddInParameter(command, "COD_USUA", DbType.String, library.User);
                this.AddInParameter(command, "DATA_HIST", DbType.DateTime, DateTime.Now);
                this.AddInParameter(command, "TXT_COME", DbType.String, library.Annotation);
                #endregion

                command.CommandText = sql.ToString();
                ExecuteNonQuery(command);
            }
        }

        #endregion
    }
}
